<template>
  <c-form :inline="true" :form="form"></c-form>
  <c-user-copies-dialog
    :copiesData="copiesData"
    :roleCopiesData="roleCopiesData"
    :submitCopies="submitCopies"
    ref="userCopiesDialog"
  ></c-user-copies-dialog>
  <c-user-task-dialog
    ref="userTaskDialog"
    :saveAssignUser="saveAssignUser"
    :businessObj="businessObj"
    :modeler="modeler"
  ></c-user-task-dialog>
</template>

<script>
import Form from "@/components/Form.vue";
import UserTaskDialog from "./GeneralComponents/userTaskDialog";
import { mapActions, mapState } from "vuex";
import { userTaskGeneral } from "@/components/bpmn/components/bpmnPropertiesPanel/forms";
import UserCopiesDialog from "./GeneralComponents/UserCopiesDialog";

export default {
  name: "UserTask_General",
  components: {
    "c-form": Form,
    "c-user-task-dialog": UserTaskDialog,
    "c-user-copies-dialog": UserCopiesDialog,
  },
  data() {
    return {
      form: userTaskGeneral.call(this),
      businessObj: null,
      copiesData: [],
      roleCopiesData: [],
    };
  },
  computed: {
    ...mapState({
      authoritiesList: (state) => state.flowDesignStore.authoritiesList,
    }),
    formatAuthorities: function() {
      if (this.authoritiesList) {
        return this.authoritiesList.reduce((rst, cur) => {
          rst[cur.label] = cur.key;
          return rst;
        }, {});
      } else {
        return [];
      }
    },
    formatReverseAuthorities: function() {
      if (this.authoritiesList) {
        return this.authoritiesList.reduce((rst, cur) => {
          rst[cur.key] = cur.label;
          return rst;
        }, {});
      } else {
        return [];
      }
    },
  },
  methods: {
    ...mapActions("flowDesignStore", ["getAuthoritiesList"]),
    // 数据初始化
    async initData() {
      let authoritiesList = this.authoritiesList;
      if (!authoritiesList) {
        const rst = await this.getAuthoritiesList({
          size: 10000,
        });
        if (!rst.status) {
          this.$message({
            type: "warning",
            message: rst.message,
          });
          authoritiesList = [];
        } else {
          authoritiesList = rst.data;
        }
      }
      this.form.items.authorities.options = this.authoritiesList;
    },
    // 数据回填
    dataReset() {
      const businessObj = this.businessObj;
      let label = "";
      if (businessObj["assignee"]) {
        label =
          businessObj["assignee"] === "${username}"
            ? "流程发起人"
            : "用户 " + businessObj["assignee"];
      } else if (businessObj["candidateUsers"]) {
        label = businessObj["candidateUsers"].split(",").length + " 候选用户";
      } else if (businessObj["candidateGroups"]) {
        label = businessObj["candidateGroups"].split(",").length + " 候选组";
      }
      this.form.items.userId.options = label ? [{ label }] : [];
      this.form.models.asynchronous = !!businessObj?.async;
      this.form.models.monopoly = !!businessObj?.exclusive;
      if (businessObj.authorities) {
        const authorities = businessObj.authorities.split(",");
        this.form.models.authorities = authorities.map(
          (item) => this.formatReverseAuthorities[item]
        );
      }
      this.copiesDataReset();
    },

    // 修改权限
    changeAuthority(val) {
      this.modeler.updateProperties({
        authorities: val.map((item) => this.formatAuthorities[item]).join(","),
      });
    },

    // 保存分配用户信息
    saveAssignUser(assignForm, type) {
      ["assignee", "candidateUsers", "candidateGroups"].forEach((item) => {
        this.modeler.updateProperties({
          [item]: undefined,
        });
      });
      this.updateProperties(assignForm, type);
    },
    updateProperties(assignForm, type) {
      const setData = {
        starter: {
          label: "流程发起人",
          key: "assignee",
          value: "${username}",
        },
        assignee: {
          label: assignForm.value.length === 1 && "用户 " + assignForm.value[0],
          key: "assignee",
          value: assignForm.value.join(","),
        },
        candidateUsers: {
          label:
            assignForm.value.length >= 1 &&
            assignForm.value.length + " 候选用户",
          key: "candidateUsers",
          value: assignForm.value.join(","),
        },
        candidateGroups: {
          label:
            assignForm.value.length >= 1 && assignForm.value.length + " 候选组",
          key: "candidateGroups",
          value: assignForm.value.join(","),
        },
      };
      const data = setData[assignForm.name];
      this.form.items.userId.options = [
        {
          key: 1,
          label: data.label || "未选择用户",
        },
      ];
      this.modeler?.updateProperties({
        [data.key]: data.value || undefined,
        assignType: type,
      });
    },

    // 抄送人弹出框信息回填
    saveCopiesData() {
      this.$refs.userCopiesDialog.visible = true;
      const choosenUser = this.businessObj.copies;
      const choosenRole = this.businessObj.roleCopies;
      if (choosenUser) {
        this.copiesData = choosenUser.split(",");
      }
      if(choosenRole){
        this.roleCopiesData = choosenRole.split(",");

      }
    },

    // 抄送人信息回填
    copiesDataReset() {
      const copies = this.businessObj.copies;
      const roleCopies = this.businessObj.roleCopies;
      const length = (copies && copies.split(",").length) || 0;
      const roleLength = (roleCopies && roleCopies.split(",").length) || 0;
      this.form.items.copies.options =
        length > 0 ? [{ key: 1, label: length + "抄送人" }] : [];
      if (roleLength > 0) {
        this.form.items.copies.options.push(
        { key: 1, label: roleLength + "业务角色" },
      );
      }
      
    },

    // 保存抄送人设置
    submitCopies() {
      const copiesData = this.$refs.userCopiesDialog.$refs.copiesField
        .copiesData;
      const roleCopiesData = this.$refs.userCopiesDialog.$refs.copiesField
        .roleCopiesData;
      this.modeler.updateProperties({
        copies: copiesData.length === 0 ? undefined : copiesData.join(","),
      });
      this.modeler.updateProperties({
        roleCopies:
          roleCopiesData.length === 0 ? undefined : roleCopiesData.join(","),
      });
      this.copiesData=copiesData
      this.roleCopiesData=roleCopiesData
      this.$refs.userCopiesDialog.visible = false;
      this.copiesDataReset();
    },
  },
};
</script>
<style scoped lang="scss">
:deep(.check-box-content) {
  display: flex !important;
  & > label {
    flex: 0 0 80px;
  }
  .el-form-item_contaner .el-checkbox-group {
    .el-checkbox {
      margin-right: 10px;
      .el-checkbox__label {
        padding-left: 5px;
      }
    }
  }
}
</style>
